package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("adminOrderController")
@Api(tags = "管理端-订单相关接口")
@Slf4j
@RequestMapping("/admin/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * 搜索订单信息
     *
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("管理端-订单搜索")
    public Result<PageResult> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO) {

        PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 订单数量统计
     *
     * @return
     */
    @GetMapping("/statistics")
    @ApiOperation("管理端-订单统计")
    public Result<OrderStatisticsVO> statistics() {
        OrderStatisticsVO orderStatisticsVO = orderService.statistics();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 管理端-获取订单详细信息
     *
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    @ApiOperation("管理端-获取订单详细信息")
    public Result<OrderVO> getByOrderId(@PathVariable Long id) {
        log.info("订单的id：{}", id);
        OrderVO orderVO = orderService.detail(id);
        return Result.success(orderVO);
    }

    /**
     * 管理端-接单
     *
     * @return
     */
    @PutMapping("/confirm")
    @ApiOperation("管理端-接单")
    public Result acceptOrders(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("接单信息:{}", ordersConfirmDTO);
        orderService.updateStatus(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 管理端-拒单
     *
     * @return
     */
    @PutMapping("/rejection")
    @ApiOperation("管理端-拒单")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        log.info("拒单信息:{}", ordersRejectionDTO);
        orderService.rejection(ordersRejectionDTO);
        return Result.success();
    }


    /**
     * 管理端-取消订单
     *
     * @return
     */
    @PutMapping("cancel")
    @ApiOperation("管理端-取消订单")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        //取消订单管理端
        orderService.cancelManager(ordersCancelDTO);
        return Result.success();
    }


    /**
     * 管理端-派送订单
     * @return
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("管理端-派送订单")
    public Result delivery(@PathVariable Long id) throws Exception {
        orderService.delivery(id);
        return Result.success();
    }

    /**
     * 管理端-完成订单
     * @param id
     * @return
     * @throws Exception
     */
    @PutMapping("complete/{id}")
    @ApiOperation("管理端-完成订单")
    public Result complete(@PathVariable Long id) throws Exception {
        orderService.complete(id);
        return Result.success();
    }
}
